<template>
	<view style="padding: 40rpx;">
		<u-toast ref="uToast"></u-toast>
		<u--form :model="form" ref="form" style="padding: 20px;" labelPosition="left">
<!-- 			<u-form-item label="款式编号:" prop="name">
				<u-input  v-model="form.clothesEncode" disabled placeholder="由系统生成" class="input" />
			</u-form-item> -->

			<u-form-item label="款式名称:" prop="address">
				<u-input v-model="form.clothesName" class="input" placeholder="请输入" />
			</u-form-item>

			<u-form-item label="图片" prop="address">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="6"></u-upload>
			</u-form-item>



		</u--form>

		<view class="bt">
			<u-button type="primary" @click="savePattern"> 保存</u-button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					id:"",
					companyId:"",
					clothesName: ""
				},
				fileList1: [],
			}
		},
		methods: {

			savePattern() {

				if (this.form.clothesName == "") {
					this.$refs.uToast.error("请输入款式名称")

					return
				}
				var that = this
				this.api.post("/clothes/newPattern", this.form).then(res => {
					console.log(res)
					that.$refs.uToast.success(res.mes)
					that.form.clothesName = ""
					uni.navigateBack({
						
					})
				})

			},




			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		},

		onLoad: function(options) {
		
		
			this.form.companyId=this.$store.state.myCompanyId		
			
            if(Object.keys(options).length!=0){
				console.log(options)

				this.form.clothesName = options.clothesName
				this.form.id=options.clothesId
			}
			
		},

	}
</script>

<style>
	.bt {
		width: 90%;
		margin: 20rpx auto;
	}

	.input {
		border-bottom: 1rpx #BBBCC6 solid;
	}
</style>
